package com.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mapper.ProductMapper;
import com.pojo.Product;
import com.pojo.ProductExample;
import com.vo.QueryVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class ProductService {
    
    @Resource
    private ProductMapper productMapper;
    
    @Transactional (propagation = Propagation.REQUIRES_NEW, readOnly = true)
    public Product queryByKey(int p_num){
        return productMapper.selectByPrimaryKey(p_num);
    }
    
    @Transactional (propagation = Propagation.REQUIRES_NEW, readOnly = true)
    public List<Product> queryByType(Product product){
        
        ProductExample example = new ProductExample();
        ProductExample.Criteria criteria = example.createCriteria();
        
        if (product != null){
            if (product.getP_id()!=null){
                criteria.andP_idEqualTo(product.getP_id());
            }
            if (product.getP_name()!=null){
                criteria.andP_nameLike("%"+product.getP_name()+"%");
            }
            if (product.getP_code()!=null){
                criteria.andP_codeLike("%"+product.getP_code()+"%");
            }
            if (product.getP_rule()!=null){
                criteria.andP_ruleLike("%"+product.getP_rule()+"%");
            }
        }
        return productMapper.selectByExample(example);
    }
    
    /**
     * 商品列表
     */
    @Transactional (propagation = Propagation.REQUIRES_NEW, readOnly = true)
    public PageInfo<Product> queryByPage(Integer pageNum , Integer pageSize , QueryVo vo) {
        
        ProductExample example = new ProductExample();
        ProductExample.Criteria criteria = example.createCriteria();
        if (vo != null) {
            if (vo.getP_id() != null && !"".equals(vo.getP_id().trim())) {
                criteria.andP_idLike("%" + vo.getP_id().trim() + "%");
            }
            if (vo.getP_name() != null && !"".equals(vo.getP_name().trim())) {
                criteria.andP_nameLike("%" + vo.getP_name().trim() + "%");
            }
            if (vo.getP_code() != null && !"".equals(vo.getP_code().trim())) {
                criteria.andP_codeLike("%" + vo.getP_code().trim() + "%");
            }
            if (vo.getP_type() != null && !"".equals(vo.getP_type().trim())) {
                criteria.andP_typeLike("%" + vo.getP_type().trim() + "%");
            }
            if (vo.getP_brand() != null && !"".equals(vo.getP_brand().trim())) {
                criteria.andP_brandLike("%" + vo.getP_brand().trim() + "%");
            }
        }
        PageHelper.startPage(pageNum , pageSize);
        List<Product> list = productMapper.selectByExample(example);
        System.out.println("list : " + list);
        return new PageInfo<>(list);
    }
    
    /**
     * 添加商品
     */
    @Transactional (propagation = Propagation.REQUIRED , rollbackFor = {Exception.class})
    public int addProduct(Product product){
        return productMapper.insertSelective(product);
    }
    
    /**
     * 删除商品
     */
    @Transactional (propagation = Propagation.REQUIRED , rollbackFor = {Exception.class})
    public int delectProduct(int id) {
        return productMapper.deleteByPrimaryKey(id);
    }
    
    /**
     * 更改商品
     */
    public int updateProduct(Product product) {
        return productMapper.updateByPrimaryKeySelective(product);
    }
    
}
